/* 
 * File:   Widget.cpp
 * Author: marcin
 * 
 * Created on 26 kwiecień 2011, 00:51
 */
#include <QSpacerItem>
#include <QFont>
#include <QDebug>
#include "Widget.h"
#include <QGridLayout>


Plot::Plot() {
   // setTitle(QString("$\\frac{1}{x^2+y^2+\\frac{1}{2}}$"));

    rosenbrock = new Rosenbrock(*this);

    rosenbrock->setMesh(50, 50);
    rosenbrock->setDomain(0, 10, 0, 10);
   // rosenbrock->setMinZ(0);
   // rosenbrock->setMaxZ(20);


   
  //  QFont serifFont("Times", 10);
   // QFont sansFont("Helvetica [Cronyx]", 12);


}

Widget::Widget() {

    
  //  this->t
  
   
    qDebug() << "widget constructor";
    pso = new PSO(30);

    QGridLayout *grid = new QGridLayout(this);

   // plot = new Plot();
    // create and show your widgets here



    mu::ParserInt int_parser;

    try {
        //  string_type sLine;
        //  std::getline(mu::console_in(), sLine);

        //      switch (CheckKeywords(sLine.c_str(), parser))
        //      {
        //      case  0: break;
        //      case  1: continue;
        //      case -1: return 0;
        //      }

        //#define MUP_EXAMPLE_INT_PARSER
        //#define MUP_EXAMPLE_COMPLEX_PARSER
#ifdef MUP_EXAMPLE_INT_PARSER
        int_parser.SetExpr(sLine);
        mu::console() << int_parser.Eval() << "\n";
        mu::console() << int_parser.Eval() << "\n";
#elif defined MUP_EXAMPLE_COMPLEX_PARSER
        cmplx_parser.SetExpr(sLine);
        mu::console() << cmplx_parser.Eval() << "\n";
#else

        // if (!sLine.length())
        //    return ;

     //   plot->rosenbrock->parser.SetExpr("log((1 - x)*(1 - x) + 100 * (y - x * x)*(y - x * x)) / 8");

        // mu::console() << std::setprecision(12);

        // The first call to eval implicitely creates the bytecode, and resets
        // an internal pointer to the bytecode parsing function. Next time you call Eval
        // the bytecode is used automatically!
        // mu::console() << "Parsing from string (slow):   " << parser.Eval() << "\n";

        // the second call automatically uses the bytecode for calculation, no interaction needed
        // mu::console() << "Parsing from bytecode (fast): " << parser.Eval() << "\n";
        //      mu::console() << parser.GetExpr() << "\n";
#endif
    } catch (mu::Parser::exception_type &e) {
        mu::console() << _T("\nError:\n");
        mu::console() << _T("------\n");
        mu::console() << _T("Message:     ") << e.GetMsg() << _T("\n");
        mu::console() << _T("Expression:  \"") << e.GetExpr() << _T("\"\n");
        mu::console() << _T("Token:       \"") << e.GetToken() << _T("\"\n");
        mu::console() << _T("Position:    ") << (int) e.GetPos() << _T("\n");
        mu::console() << _T("Errc:        ") << std::dec << e.GetCode() << _T("\n");
    }





    this->label = new QLabel();
    this->label->setText(QString("f(x,y)="));
    grid->addWidget(label, 0, 0);

    this->lineedit = new QLineEdit();
    grid->addWidget(lineedit, 0, 1);

    this->button = new QPushButton();
    this->button->setText(QString("Rysuj"));
    grid->addWidget(button, 0, 2);

    connect(button, SIGNAL(clicked()),
            this, SLOT(Rysuj()));

    this->iteruj = new QPushButton();
    this->iteruj->setText(QString("Iteruj"));
    grid->addWidget(iteruj, 0, 3);

    connect(iteruj, SIGNAL(clicked()),
            this, SLOT(Iteruj()));

    // QSpacerItem* spacer=new QSpacerItem();
    grid->addWidget(plot, 1, 0, 1, 3);
    this->memo = new QTextEdit();
    memo->setGeometry(0, 0, 200, 200);
    grid->addWidget(memo, 2, 0, 1, 3);


    

    resize(800, 1000);
   // plot->rosenbrock->create();
   // plot->resize(600, 570);

    //  plot->show();
   // plot->updateData();
   // plot->updateGL();


}

Widget::~Widget() {
}

void Widget::Rysuj() {
    qDebug() << "ok";
  //  plot->rosenbrock->parser.SetExpr(this->lineedit->text().toAscii().data());

   

//    plot->setMeshLineWidth(1);
//    plot->coordinates()->setGridLinesColor(RGBA(0, 0, 0.5));
//    plot->coordinates()->setLineWidth(1);
//    plot->coordinates()->setNumberColor(RGBA(0, 0.5, 0));
//    plot->coordinates()->setNumberFont("Courier", 10);
//    plot->setTitleFont("Courier", 11);
//    plot->coordinates()->setLabelFont("Courier", 12, QFont::Bold);


    pso->maximize = false;

    pso->parser.SetExpr(this->lineedit->text().toAscii().data());

    pso->xmin = 0;
    pso->xmax = 10.0;
    pso->ymin = 0;
    pso->ymax = 10.0;

    pso->vmax = 0.2;
    pso->c_p = 2.8;
    pso->c_g = 1.3;

    pso->x_width = 10.0;
    pso->y_width = 10.0;

    qDebug() << "przed inicjalizacja";

   

    qDebug() << "po inicjalizacji";


    pso->fitness_gbest = pso->particles_vec[0]->fitness;

    pso->gbest_pos[0] = pso->particles_vec[0]->pbest_pos[0];
    pso->gbest_pos[1] = pso->particles_vec[0]->pbest_pos[1];

    plot->data_wek.clear();
    qDebug() << "183";
    for (int i = 0; i < pso->particles_vec.size(); i++) {

       plot->data_wek.push_back(new Point3d(pso->particles_vec[i]->x_pos[0], pso->particles_vec[i]->x_pos[1], pso->particles_vec[i]->fitness));

    }




    //    for (float i = -2; i < 2; i += 0.2) {
    //
    //        for (float j = -2; j < 2; j += 0.2) {
    //
    //            plot->rosenbrock->xVal = i;
    //            plot->rosenbrock->yVal = j;
    //            plot->data_wek.push_back(new Point3d(i, j, plot->rosenbrock->parser.Eval()));
    //
    //        }
    //
    //    }

    plot->rosenbrock->create();
    
    plot->setRotation(30, 0, 15);
    plot->setScale(1, 1,1);
    plot->setShift(0, 0, 0);
    plot->setZoom(0.9);

    for (unsigned i = 0; i != plot->coordinates()->axes.size(); ++i) {
        plot->coordinates()->axes[i].setMajors(10);
        plot->coordinates()->axes[i].setMinors(5);
       // coordinates()->axes[i].setLabelColor(RGBA(0,0,0.4));
    }
    
    
    
    plot->coordinates()->axes[X1].setLabelString("x-axis");
    plot->coordinates()->axes[Y1].setLabelString("y-axis");
    
    
    plot->setMeshLineWidth(1);
	plot->coordinates()->setGridLinesColor(RGBA(0,0,0.5));
	plot->coordinates()->setLineWidth(1);
	plot->coordinates()->setNumberColor(RGBA(0,0.5,0));
	plot->coordinates()->setNumberFont("Courier",10);
	plot->setTitleFont("Courier",11);
	plot->coordinates()->setLabelFont("Courier",12, QFont::Bold);
	plot->coordinates()->axes[X1].setLabelString("X1");
	plot->coordinates()->axes[Y1].setLabelString("Y1");
	plot->coordinates()->axes[Z1].setLabelString("Z1");
	plot->coordinates()->axes[X2].setLabelString("X2");
	plot->coordinates()->axes[Y2].setLabelString("Y2");
	plot->coordinates()->axes[Z2].setLabelString("Z2");
	plot->coordinates()->axes[X3].setLabelString("X3");
	plot->coordinates()->axes[Y3].setLabelString("Y3");
	plot->coordinates()->axes[Z3].setLabelString("Z3");
	plot->coordinates()->axes[X4].setLabelString("X4");
	plot->coordinates()->axes[Y4].setLabelString("Y4");
	plot->coordinates()->axes[Z4].setLabelString("Z4");
    
    
    
    
    plot->setCoordinateStyle(BOX);
    plot->updateData();
    plot->updateGL();
}

void Widget::Iteruj() {

    for (int i = 0; i < 30; i++) {








        plot->data_wek.clear();
        pso->Iteruj();
        qDebug() << "183";
        for (int i = 0; i < pso->particles_vec.size(); i++) {

           plot->data_wek.push_back(new Point3d(pso->particles_vec[i]->x_pos[0], pso->particles_vec[i]->x_pos[1], pso->particles_vec[i]->fitness));

        }




        //    for (float i = -2; i < 2; i += 0.2) {
        //
        //        for (float j = -2; j < 2; j += 0.2) {
        //
        //            plot->rosenbrock->xVal = i;
        //            plot->rosenbrock->yVal = j;
        //            plot->data_wek.push_back(new Point3d(i, j, plot->rosenbrock->parser.Eval()));
        //
        //        }
        //
        //    }

        //  plot->rosenbrock->create();
        plot->updateData();
        plot->updateGL();

    }

}
